<template>
<div id="page">
    <div id="content">
        <el-form id="box">
            <h1 style="font-weight: 400; font-size: 32px">注册账号</h1>
            <p style="color: #707070;">请选择您在日常教学中的实际身份</p>
            <a href="JavaScript:;" @click="teacher()">
                <img src="../assets/Register/teacher.png" />
                <span>我是老师/助教</span>
            </a>
            <a href="JavaScript:;" @click="student()">
                <img src="../assets/Register/student.png" />
                <span>我是学生</span>
            </a>
            <p class="login" @click="login()">
                <span>已有账号？</span>
                <span style="color: #32BAF0">去登录</span>
            </p>
        </el-form>
        <!-- 注册老师账号 -->
        <el-form :model="formTeacher" ref="formTeacher" id="boxTeacher" v-bind:class="messageTeacher" :rules="rules">
            <a href="JavaScript:;" @click="returnReg()"><img src="../assets/Register/returnReg.png" alt="" /></a>

            <span>老师/助教注册</span>
            <el-form-item prop="account">
                <el-input v-model="formTeacher.account" placeholder="邮箱/手机"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="formTeacher.password" placeholder="密码" type="password"></el-input>
            </el-form-item>
            <el-form-item prop="checkPassword">
                <el-input v-model="formTeacher.checkPassword" placeholder="确认密码" type="password"></el-input>
            </el-form-item>
            <el-form-item prop="username">
                <el-input v-model="formTeacher.username" placeholder="姓名"></el-input>
            </el-form-item>
            <el-form-item prop="school">
                <el-input v-model="formTeacher.school" placeholder="学校"></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="formTeacher.verification" placeholder="验证码"></el-input>
            </el-form-item>
            <el-button type="primary" style="background: #32baf0;" @click="submitTeacher()"><span style="font-size:16px; padding-top:20px">注册成为老师/助教</span></el-button>
            <p class="login" @click="login()">
                <span style="font-size:14px">已有账号？</span>
                <span style="color: #32BAF0; font-size:14px">去登录</span>
            </p>
        </el-form>

        <!-- 注册学生账号 -->
        <el-form :model="formStudent" ref="formStudent" id="boxStudent" v-bind:class="messageStudent" :rules="rules">
            <a href="JavaScript:;" @click="returnReg()"><img src="../assets/Register/returnReg.png" alt="" /></a>

            <span>学生注册</span>
            <el-form-item prop="account">
                <el-input v-model="formStudent.account" placeholder="邮箱/手机"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="formStudent.password" placeholder="密码" type="password"></el-input>
            </el-form-item>
            <el-form-item prop="checkPassword">
                <el-input v-model="formStudent.checkPassword" placeholder="确认密码" type="password"></el-input>
            </el-form-item>
            <el-form-item prop="username">
                <el-input v-model="formStudent.username" placeholder="姓名"></el-input>
            </el-form-item>
            <el-form-item prop="id">
                <el-input v-model="formStudent.id" placeholder="学号"></el-input>
            </el-form-item>
            <el-form-item prop="school">
                <el-input v-model="formStudent.school" placeholder="学校"></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="formStudent.verification" placeholder="验证码"></el-input>
            </el-form-item>
            <el-button type="primary" style="background: #32baf0;" @click="submitStudent()"><span style="font-size:16px; padding-top:20px">注册成为学生</span></el-button>
            <p class="login" @click="login()">
                <span style="font-size:14px">已有账号？</span>
                <span style="color: #32BAF0; font-size:14px">去登录</span>
            </p>
        </el-form>
    </div>
</div>
</template>

<style>
#page {
    width: 1518px;
    height: 720px;
    margin: 0 auto;
}

#page #content {
    width: 1518px;
    height: 720px;
    background-image: url(../assets/Register/reg-bg-01.jpg) !important;
    position: fixed;
}

#page #content #box {
    width: 480px;
    height: 504px;
    margin: 100px auto;
    padding: 47px 41px 0;
    background: #fff;
    box-sizing: border-box;
    border-radius: 4px;
}

#page #content #box a img {
    display: inline-block;
    position: relative;
    top: 20px;
}

#page #content #box a span {
    display: inline-block;
    width: 330px;
    line-height: 64px;
    text-align: center;
    margin-left: 8px;
    color: white;
    background: #32baf0;
}

#page #content .login {
    float: right;
    cursor: pointer;
}

.showTeacher,
.showStudent {
    display: none;
}

#page #content #boxTeacher,
#page #content #boxStudent {
    width: 480px;
    height: 604px;
    margin: 50px auto;
    padding: 47px 41px 0;
    background: #fff;
    box-sizing: border-box;
    border-radius: 4px;
    position: relative;
    top: -630px;
}

#page #content #boxTeacher span,
#page #content #boxStudent span {
    display: inline-block;
    position: relative;
    top: -10px;
    font-size: 32px;
    font-weight: 400;
}

#page #content #boxTeacher .el-form-item__content,
#page #content #boxStudent .el-form-item__content {
    margin-left: 10px !important;
}

#page #content #boxTeacher .el-button--primary,
#page #content #boxStudent .el-button--primary {
    text-align: center;
    margin-left: 10px;
    width: 390px;
    height: 48px;
}
</style>

<script>
export default {
    data() {
        return {
            messageTeacher: {
                showTeacher: true
            },
            messageStudent: {
                showStudent: true
            },
            formTeacher: {
                account: "",
                password: "",
                checkPassword: "",
                username: "",
                school: "",
                verification: "",
                role: ""
            },
            formStudent: {
                account: "",
                password: "",
                checkPassword: "",
                username: "",
                id: "",
                school: "",
                verification: "",
                role: ""
            },
            rules: {
                account: [
                    { required: true, message: "邮箱/手机不能为空", trigger: "blur" }
                ],
                password: [
                    { required: true, message: "密码不能为空", trigger: "blur" }
                ],
                checkPassword: [
                    { required: true, message: "再次输入密码不能不空", trigger: "blur" }
                ],
                username: [
                    { required: true, message: "姓名不能为空", trigger: "blur" },
                    {
                        pattern: /^[\u4E00-\u9FA5]{2,5}$/,
                        message: "学生姓名不合法",
                        trigger: "blur"
                    }
                ],
                school: [
                    { required: true, message: "学校不能为空", trigger: "blur" },
                    {
                        pattern: /^[\u4E00-\u9FA5]{4,}$/,
                        message: "学校名称不合法",
                        trigger: "blur"
                    }
                ],
                id: [
                    { required: true, message: "学号不能为空", trigger: "blur" },
                    {
                        pattern: /^[0-9]{11}$/,
                        message: "学生的学号为11位数字",
                        trigger: "blur"
                    }
                ]
            }
        };
    },
    methods: {
        login: function () {
            this.$router.push({ path: "/login" });
        },
        teacher: function () {
            this.formTeacher.role = "t";
            this.messageTeacher.showTeacher = false;
        },
        student: function () {
            this.formStudent.role = "s";
            this.messageStudent.showStudent = false;
        },
        returnReg: function () {
            this.$refs.formTeacher.resetFields();
            this.$refs.formStudent.resetFields();
            this.messageTeacher.showTeacher = true;
            this.messageStudent.showStudent = true;
        },
        submitTeacher: function (formTeacher) {
            this.$refs.formTeacher.validate(valid => {
                if (valid) {
                    console.log(formTeacher);
                    console.log(this.formTeacher.account);
                    if (this.formTeacher.password != this.formTeacher.checkPassword) {
                        this.$message({ message: "输入的密码不一致", type: "error" });
                        return;
                    }
                    this.$axios
                        .get("http://localhost:8080/user/log_on", {
                            params: {
                                mail: this.formTeacher.account,
                                password: this.formTeacher.password,
                                name: this.formTeacher.username,
                                school: this.formTeacher.school,
                                role: this.formTeacher.role
                            }
                        })
                        .then(response => {
                            console.log(response.data);
                            if (response.data === "ok") {
                                this.$message.success("注册成功");
                                // this.$confirm("是否跳转登录页面?", "提示", {
                                //   confirmButtonText: "确定",
                                //   cancelButtonText: "取消",
                                //   type: "warning"
                                // })
                                //   .then(() => {
                                //     window.location.href = "/login";
                                //   })
                                //   .catch(() => {
                                //     this.$message({
                                //       type: "info",
                                //       message: "已取消删除"
                                //     });
                                //   });
                            } else if (response.data === "invalid mail or tel") {
                                this.$message.error("输入的电话号或邮箱格式不合法");
                            }
                        })
                        .catch(function (error) {
                            // 请求失败处理
                            console.log(error);
                        });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        submitStudent: function (formStudent) {
            this.$refs.formStudent.validate(valid => {
                if (valid) {
                    console.log(formStudent);
                    if (this.formStudent.password !== this.formStudent.checkPassword) {
                        this.$message({ message: "输入的密码不一致", type: "error" });
                        return;
                    }
                    this.$axios
                        .get("http://localhost:8080/user/log_on", {
                            params: {
                                mail: this.formStudent.account,
                                password: this.formStudent.password,
                                name: this.formStudent.username,
                                school: this.formStudent.school,
                                role: this.formStudent.role
                            }
                        })
                        .then(response => {
                            console.log(response.data);
                            if (response.data === "ok") {
                                this.$message.success("注册成功");
                                // this.$confirm("是否跳转登录页面?", "提示", {
                                //   confirmButtonText: "确定",
                                //   cancelButtonText: "取消",
                                //   type: "warning"
                                // })
                                //   .then(() => {
                                //     window.location.href = "/login";
                                //   })
                                //   .catch(() => {
                                //     this.$message({
                                //       type: "info",
                                //       message: "已取消删除"
                                //     });
                                //   });
                            } else if (response.data === "invalid mail or tel") {
                                this.$message.error("输入的电话号或邮箱格式不合法");
                            }
                        })
                        .catch(function (error) {
                            // 请求失败处理
                            console.log(error);
                        });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        }
    }
};
</script>
